package io.unicorn.modules.water.service.impl;

import io.unicorn.modules.sys.entity.SysUserEntity;
import io.unicorn.modules.sys.shiro.ShiroUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.unicorn.common.utils.PageUtils;
import io.unicorn.common.utils.Query;

import io.unicorn.modules.water.dao.VaccountmanageDao;
import io.unicorn.modules.water.entity.VaccountmanageEntity;
import io.unicorn.modules.water.service.VaccountmanageService;


@Service("vaccountmanageService")
public class VaccountmanageServiceImpl extends ServiceImpl<VaccountmanageDao, VaccountmanageEntity> implements VaccountmanageService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key   = (String)params.get("key");
        String value = (String)params.get("value");

        String deptId = (String)params.get("dept");
        Map<String , Object> queryMap = new HashMap<>();

        if(StringUtils.isNotBlank(deptId) && StringUtils.isNotEmpty(deptId))
        {
            String[] tmp = deptId.split("_");
            switch(tmp.length){
                case 1:
                    queryMap.put("city_id",tmp[0]);
                    break;
                case 2:
                    queryMap.put("city_id",tmp[0]);
                    queryMap.put("area_id",tmp[1]);
                    break;
                case 3:
                    queryMap.put("city_id",tmp[0]);
                    queryMap.put("area_id",tmp[1]);
                    queryMap.put("unit_name_w_id",tmp[2]);
                    break;
            }
        }

        //2025.5.31 超期数据关闭查看
        SysUserEntity user = ShiroUtils.getUserEntity();
        Integer intViewData = user.getViewData();

        IPage<VaccountmanageEntity> page = this.page(
                new Query<VaccountmanageEntity>().getPage(params),
                new QueryWrapper<VaccountmanageEntity>()
                        .allEq(queryMap!=null && queryMap.size() > 0, queryMap, false)
                        .like(StringUtils.isNotBlank(value) && StringUtils.isNotBlank(key), key, value)
                        .apply(intViewData != 1,"pro_name not  in (select 小区名称 from [Gather_DB].[dbo].[v_stop_view_data])")
        );

        return new PageUtils(page);
    }

}
